package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ruoyi.system.domain.TimeSlot;
import com.ruoyi.system.domain.YyAppointment;
import com.ruoyi.system.mapper.YyAppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YyAppointmentTimeMapper;
import com.ruoyi.system.domain.YyAppointmentTime;
import com.ruoyi.system.service.IYyAppointmentTimeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 预约时间段Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-25
 */
@Service
public class YyAppointmentTimeServiceImpl implements IYyAppointmentTimeService
{
    @Autowired
    private YyAppointmentTimeMapper yyAppointmentTimeMapper;
    @Autowired
    private YyAppointmentMapper yyAppointmentMapper;

    /**
     * 查询预约时间段
     *
     * @param id 预约时间段主键
     * @return 预约时间段
     */
    @Override
    public YyAppointmentTime selectYyAppointmentTimeById(Long id)
    {
        return yyAppointmentTimeMapper.selectYyAppointmentTimeById(id);
    }

    /**
     * 查询预约时间段列表
     *
     * @param yyAppointmentTime 预约时间段
     * @return 预约时间段
     */
    @Override
    public List<YyAppointmentTime> selectYyAppointmentTimeList(YyAppointmentTime yyAppointmentTime)
    {
        return yyAppointmentTimeMapper.selectYyAppointmentTimeList(yyAppointmentTime);
    }

    /**
     * 新增预约时间段
     *
     * @param yyAppointmentTime 预约时间段
     * @return 结果
     */
    @Override
    public int insertYyAppointmentTime(YyAppointmentTime yyAppointmentTime)
    {
        return yyAppointmentTimeMapper.insertYyAppointmentTime(yyAppointmentTime);
    }

    /**
     * 修改预约时间段
     *
     * @param yyAppointmentTime 预约时间段
     * @return 结果
     */
    @Override
    public int updateYyAppointmentTime(YyAppointmentTime yyAppointmentTime)
    {
        return yyAppointmentTimeMapper.updateYyAppointmentTime(yyAppointmentTime);
    }

    /**
     * 批量删除预约时间段
     *
     * @param ids 需要删除的预约时间段主键
     * @return 结果
     */
    @Override
    public int deleteYyAppointmentTimeByIds(String ids)
    {
        return yyAppointmentTimeMapper.deleteYyAppointmentTimeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预约时间段信息
     *
     * @param id 预约时间段主键
     * @return 结果
     */
    @Override
    public int deleteYyAppointmentTimeById(Long id)
    {
        return yyAppointmentTimeMapper.deleteYyAppointmentTimeById(id);
    }

    /**
     * 查询今天哪个时间端还能预约
     */
    @Override
    public List<Map<String, Object>> selectAppointmentTime(Integer doctorId, Integer departmentId) {
        Date date = new Date();
        if (doctorId == null && departmentId == null){
            throw new RuntimeException("参数错误");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = dateFormat.format(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<YyAppointmentTime> yyAppointmentTimes = yyAppointmentTimeMapper.selectYyAppointmentTimeList(new YyAppointmentTime());
        List<YyAppointmentTime> yyAppointmentTimes = yyAppointmentTimeMapper.selectYyAppointment(doctorId,departmentId);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < yyAppointmentTimes.size(); i++) {
            YyAppointmentTime yyAppointmentTime = yyAppointmentTimes.get(i);
            String[] split = yyAppointmentTime.getAppointmentTime().split("-");
            String d1 = split[0];
            String d2 = split[1];
            String s3 = str + " " + d2 + ":00";
            try {
                Date parse = format.parse(s3);
                if (parse.getTime() > date.getTime()){
                    if(StrUtil.isEmpty(yyAppointmentTime.getStatus()) && yyAppointmentTime.getDepartmentId() == departmentId){
                        Map<String, Object> stringObjectHashMap = new HashMap<>();
                        stringObjectHashMap.put("time",yyAppointmentTime.getAppointmentTime());
                        list.add(stringObjectHashMap);
                    }
                }
            } catch (ParseException e) {
                throw new RuntimeException("系统错误");
            }
        }
        return list;
    }

    @Override
    public void updateBatch(List<YyAppointmentTime> yyAppointmentTimes) {
        yyAppointmentTimeMapper.updateBatch(yyAppointmentTimes);
    }

    @Override
    public List<Map<String, Object>> selectAppointmentTime2(Long doctorId, Integer departmentId, String date) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = new HashMap<>();
            if (i == 0){
                map.put("time","08:30-09:00");
                list.add(map);
            }else if (i == 1){
                map.put("time","09:00-09:30");
                list.add(map);
            }else if (i == 2){
                map.put("time","09:30-10:00");
                list.add(map);
            }else if (i == 3){
                map.put("time","10:00-10:30");
                list.add(map);
            }else if (i == 4){
               map.put("time","10:30-11:00");
                list.add(map);
            }else if (i == 5){
                map.put("time","14:30-15:00");
                list.add(map);
            }else if (i == 6){
                map.put("time","15:00-15:30");
                list.add(map);
            }else if (i == 7){
                map.put("time","15:30-16:00");
                list.add(map);
            }else if (i == 8){
                map.put("time","16:00-16:30");
                list.add(map);
            }else if (i == 9){
                map.put("time","16:30-17:00");
                list.add(map);
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = simpleDateFormat.parse(date);
            List<YyAppointment> yyAppointments = yyAppointmentMapper.selectAllYyAppointmentList2(doctorId,departmentId,parse);
            for (int i = 0; i < yyAppointments.size(); i++) {
                YyAppointment yyAppointment = yyAppointments.get(i);
                String yyTime = yyAppointment.getYyTime();
                Map<String, Object> map = new HashMap<>();
                map.put("time",yyTime);
                list.remove(map);
            }
            Date time = new Date();
            if (date.equals(simpleDateFormat.format(time)) ){
                String json = "{\"msg\":\"操作成功\",\"code\":0,\"data\":[{\"time\":\"08:30-09:00\"},{\"time\":\"09:00-09:30\"},{\"time\":\"09:30-10:00\"},{\"time\":\"10:00-10:30\"},{\"time\":\"10:30-11:00\"},{\"time\":\"14:30-15:00\"},{\"time\":\"15:00-15:30\"},{\"time\":\"15:30-16:00\"},{\"time\":\"16:00-16:30\"},{\"time\":\"16:30-17:00\"}]}";

                Gson gson = new Gson();
                JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
                JsonArray dataArray = jsonObject.getAsJsonArray("data");

                List<TimeSlot> timeSlots = new ArrayList<>();
                for (int i = 0; i < dataArray.size(); i++) {
                    JsonObject dataObject = dataArray.get(i).getAsJsonObject();
                    TimeSlot timeSlot = gson.fromJson(dataObject, TimeSlot.class);
                    timeSlots.add(timeSlot);
                }

                LocalTime currentTime = LocalTime.now();
                List<TimeSlot> remainingTimeSlots = new ArrayList<>();

                for (TimeSlot timeSlot : timeSlots) {
                    String[] times = timeSlot.getTime().split("-");
                    LocalTime startTime = LocalTime.parse(times[0]);
                    LocalTime endTime = LocalTime.parse(times[1]);

                    if (currentTime.isBefore(endTime)) {
                        remainingTimeSlots.add(timeSlot);
                    }
                }
                List<Map<String, Object>> list3 = new ArrayList<>();
                for (TimeSlot timeSlot : remainingTimeSlots) {
                    Map<String, Object> hashMap = new HashMap<>();
                    hashMap.put("time",timeSlot.getTime());
                    list3.add(hashMap);
                }
                List<Map<String, Object>> toRemove = new ArrayList<>();
                for (Map<String, Object> map : list3) {
                    for (int i = 0; i < yyAppointments.size(); i++) {
                        YyAppointment yyAppointment = yyAppointments.get(i);
                        if (map.get("time").equals(yyAppointment.getYyTime())){
                            toRemove.add(map);
                        }
                    }
                }
                list3.removeAll(toRemove);
//                list3.forEach(map1 -> {
//                    for (int i = 0; i < yyAppointments.size(); i++) {
//                        YyAppointment yyAppointment = yyAppointments.get(i);
//                        if (map1.get("time").equals(yyAppointment.getYyTime())){
//                            list3.remove(map1);
//                        }
//                    }
//                });

                // 打印剩余的时间段
                for (TimeSlot timeSlot : remainingTimeSlots) {
                    System.out.println(timeSlot.getTime());
                }

                return list3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return list;
    }
}
